                    //
//  ScrollViewCell.m
//  HeadTableView
//
//  Created by 千锋 on 15/12/15.
//  Copyright (c) 2015年 1000phone. All rights reserved.
//

#import "ScrollViewCell.h"
#import "ScrollmageDataModel.h"

#import "UIImageView+WebCache.h"

#define PAGE_CONTROL_TAG 20
#define SCROL_CONTROL_TAG 10

@interface ScrollViewCell() < UIScrollViewDelegate>

@end

@implementation ScrollViewCell
{
    UIScrollView *_scrollView;
    UIView *_backgroundView;
    UILabel *_title;
    UIPageControl *_pageControll;
    ScrollmageDataModel *_scrolData;
    
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

// 重写init方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

- (void)createUI
{
    CGSize cellSize = self.bounds.size;
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, cellSize.width, cellSize.height)];
    [self.contentView addSubview:_scrollView];
    
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.delegate = self;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _pageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(160, 220, 200, 30)];
    _pageControll.numberOfPages = _imageNameArray.count;
    _pageControll.currentPage = 0;
    _pageControll.pageIndicatorTintColor = [UIColor grayColor];
    _pageControll.currentPageIndicatorTintColor = [UIColor redColor];
    [_pageControll addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    _pageControll.tag = PAGE_CONTROL_TAG;
    [self.contentView addSubview:_pageControll];
}

#pragma mark- pagecontroll
- (void)pageChange:(UIPageControl *)sender
{
    NSInteger currentPage = sender.currentPage;
    UIScrollView *scrollView = [self.contentView viewWithTag:SCROL_CONTROL_TAG];
    CGSize sreenSize = [UIScreen mainScreen].bounds.size;
    CGPoint contentOfset = CGPointMake(currentPage *sreenSize.width, 0);
    [_scrollView setContentOffset:contentOfset animated:YES];
}
- (void)setImageNameArray:(NSArray *)imageNameArray
{
    _imageNameArray = imageNameArray;
    _pageControll.numberOfPages = _imageNameArray.count;
    CGFloat imageWidth = CGRectGetWidth(_scrollView.frame);
    CGFloat imageHeight = CGRectGetHeight(_scrollView.frame);
    
    for (int index = 0; index < self.imageNameArray.count; index++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(index*imageWidth, 0, imageWidth, imageHeight)];
        _scrolData = [[ScrollmageDataModel alloc] init];
        _scrolData = _imageNameArray[index];
      
        [imageView sd_setImageWithURL:[NSURL URLWithString:_scrolData.o_image] placeholderImage:[UIImage imageNamed:@"2"]];
        _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, self.frame.size.width, 50)];
        _title.text = _scrolData.title;
        _title.numberOfLines = 0;
        _title.font = [UIFont boldSystemFontOfSize:17];
        _title.textColor = [UIColor whiteColor];
        _title.lineBreakMode = NSLineBreakByWordWrapping;
        CGSize size = [_title sizeThatFits:CGSizeMake(_title.frame.size.width, 50)];
        _title.frame = CGRectMake(5, 200, imageWidth , 50);
        
        _title.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
        
        [imageView addSubview:_title];
        // 将图片视图添加到UIScrollView中
        [_scrollView addSubview:imageView];
    
    _scrollView.contentSize = CGSizeMake(imageWidth*self.imageNameArray.count, imageHeight);
    }
}

- (void)layoutSubviews
{
    
    // 更新UIScrollView的尺寸
    _scrollView.frame = self.frame;
    // 更新UIScrollView子视图的尺寸
    CGFloat imageWidth = CGRectGetWidth(_scrollView.frame);
    CGFloat imageHeight = CGRectGetHeight(_scrollView.frame);
    for (int index = 0; index < _scrollView.subviews.count; index++) {
        // 取出子视图
        UIView * view = _scrollView.subviews[index];
        view.frame = CGRectMake(index*imageWidth, 0, imageWidth, imageHeight);
    }
    // 更新UIScrollView的contentSize
    _scrollView.contentSize = CGSizeMake(imageWidth*_imageNameArray.count, imageHeight);
    
}

#pragma mark - uiscrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
    UIPageControl *pageControl = [self.contentView viewWithTag:PAGE_CONTROL_TAG];
    pageControl.currentPage = currentPage;
}
@end
